<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Object_m extends Base_model {

	//protected $_table_name = 'object';
	//protected $_key_name = 'object_id';

	public function __construct() {

		parent::__construct();

	}

	function get_fields() {
	   return $this->db->list_fields('h_object');
    }

	//配對
	function match_object_rows($where__da, $limit = false) {

		//print_r($where__da);

		$test_flag = false;

		$where_str = '';
		$where = '';
		$where_flag = TRUE;
		

		$excerpt_sql='';
		$record_sql_name ='';
		$i = 1;

		if ($limit == false) {
			 $limit = '0 , 10';
		}

		$CI = & get_instance();
		
		
		$this -> db -> order_by("ctime", "DESC");


		$query = $this -> db -> query("SELECT * from `h_object` " . $where . ' limit ' . $limit);
		//return $query;
		return 1;
	}







	function match_object($where__da, $limit = false) {
		
		

		$test_flag = false;

		$where_str = '';
		$where = '';
		$where_flag = TRUE;
		$md5_time=md5(time());

		
		$excerpt_sql_data=array(
						'demand_id'=>$where__da['demand_id'],
						'verify_code'=>md5(time()) ,
						'obj_dem'=>'object' ,
						'flag'=>'Main',
						'excerpt_sql'=>md5(time()) 
						);
		
		//$this->db->insert( 'h_excerpt_sql' , $excerpt_sql_data);

		$excerpt_sql='';
		$record_sql_name ='';
		$i = 1;

		//			[demand]					[object]
		//---------------------------------------------------
		//指定大樓1	- specify_building1 	|	building_name
		//指定大樓2 	- specify_building2		|	building_name
		//---------------------------------------------------		
		//重點區域1	－key_areas1				|	area
		//重點區域2	－key_areas2				|	area
		//---------------------------------------------------------------		
		//路段 		－sections1				|	house_building
		//路段 		－sections2				|	house_building
		//---------------------------------------------------------------	
		//物件案名 	object_name1			|	case_name
		//物件案名	object_name2			|	case_name
		//---------------------------------------------------------------	
		//行政區 		administrative_area1	｜	administrative_area
		//行政區 		administrative_area2	｜	administrative_area
		//---------------------------------------------------------------				
		//

		
		
		
		foreach ($where__da as $name => $value) {

			if ($value != '' AND $value != '0' AND $value != '0.00') {
				
				if ($name == "total_budget1") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= "  `" . 'sale_total' . "` >= '" . $value . "' ";

				} elseif ($name == "total_budget2") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= "  `" . 'sale_total' . "` <= '" . $value . "' ";

				} elseif ($name == 'sections1') {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " OR ";
					}

					$where .= "  `" . 'house_building' . "` like '%" . $value . "%' ";

				} elseif ($name == 'sections2') {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " OR ";
					}

					$where .= "  `" . 'house_building' . "` like '%" . $value . "%' ";

				} elseif ($name == 'indoor_area1') {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= "  `" . 'main_building_area' . "` >= '" . $value . "' ";

				} elseif ($name == 'indoor_area2') {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= "  `" . 'main_building_area' . "` <= '" . $value . "' ";

				} elseif ($name == "land_area1") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= "  `" . 'stakeholders_land_area' . "` >= '" . $value . "' ";

				} elseif ($name == "land_area2") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= "  `" . 'stakeholders_land_area' . "` <= '" . $value . "' ";

				} elseif ($name == "budget_per_ping1") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= "  `" . 'sale_land_unit_price' . "` >= '" . $value . "' ";

				} elseif ($name == "budget_per_ping2") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= "  `" . 'sale_land_unit_price' . "` <= '" . $value . "' ";

				} elseif (strstr($name, "building_area1") == true) {

					$vvv = @floor($value);

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= "  `" . 'building_area' . "` >= '" . $vvv . "' ";

				} elseif ($name == "building_area2") {

					$vvv2 = floor($value);

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= "  `" . 'building_area' . "` <= '" . $vvv2 . "' ";

				} elseif ($name == "key_areas1") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " OR ";
					}

					$where .= "  `" . 'area' . "` like '%" . $value . "%' ";

				} elseif ($name == "key_areas2") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " OR ";
					}

					$where .= "  `" . 'area' . "` like '%" . $value . "%' ";

				} elseif ($name == "specify_building1") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " OR ";
					}

					$where .= "  `" . 'building_name' . "` like '%" . $value . "%' ";

				} elseif ($name == "specify_building2") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " OR ";
					}

					$where .= "  `" . 'building_name' . "` like '%" . $value . "%' ";
				}

			}
		}	
		

		$CI = & get_instance();
		//$CI->login_lib->users_id();
		
		//$CI->login_lib->users_name();

		
		/*/$record_sql_name
		$this -> db -> insert('h_sql', 
				array(	'SQL' => $record_sql_name ,
						'type' => 'from object_m ',
						'ctime' => time(),
						'create_id'=>$CI->login_lib->users_id() ,
						'create_name'=> $CI->login_lib->users_name()));
		*/
		
		
 		
 		$run_sql = "SELECT * from `h_object` " .$where ;//. ' limit ' . $limit ;
 		
		
		$this -> db -> insert('h_sql', 
				array(	'SQL' => $run_sql, 
						'type' => '－需求－＞配 物件－',
						'ctime' => time(),
						'create_id'=>$CI->login_lib->users_id() ,
						'create_name'=> $CI->login_lib->users_name() )
						);
		
		
		//$this -> db -> order_by("ctime", "DESC");
		//$query = $this->db->get("object");

		$query = $this -> db -> query($run_sql);
		return $query;
	}


	function excerpt_object($verify_code) {

		$sql   ="SELECT * from `h_excerpt_sql` WHERE `verify_code` = '".$verify_code."' AND `flag` = 'Y' ";
		$query = $this -> db -> query($sql);

		$_where_sql='';
		$_sql_flag=true;

		foreach( $query->result() as $row ){
			if($_sql_flag){

				$_sql_flag=false;
				$_where_sql.=' WHERE '.$row->excerpt_sql;

			}else{

				if($row->field=='case_name'){
					$_where_sql.=' OR '.$row->excerpt_sql;
				}else{
					$_where_sql.=' AND '.$row->excerpt_sql;
				}

			}
		}

		$object_sql = 'SELECT * from `h_object` '.$_where_sql;	




		
		$CI = & get_instance();			
		$this->db->insert('h_sql', array(
									"SQL"=>$object_sql , 
									'type'=>'excerpt SQL query h_object',
									'ctime' => time(),
									'create_id'=>$CI->login_lib->users_id() ,
									'create_name'=> $CI->login_lib->users_name()
									));

		$demand_query = $this -> db -> query($object_sql);
		return $demand_query;

	}

	
	function excerpt_object_field($verify_code) {		
		$sql   ="SELECT   distinct field , explanation from `h_excerpt_sql` WHERE `verify_code` = '".$verify_code."' AND `flag` = 'Y' ";
		$query = $this -> db -> query($sql);
		return $query;
	}

	
	function display_object($object_id_s) {
			
		$_v_flag=true;
		$_v_whr='';
			
		foreach($object_id_s as $_k => $_v){
			
			if($_v_flag){
				
				$_v_flag=false;
				$_v_whr.=" WHERE `object_id` != '".$_v."'";
						
			}else{
				$_v_whr.=" AND `object_id` != '".$_v."'";
			}
		}					
				
		$sql   ="SELECT   * from `h_object` ".$_v_whr;
		$query = $this -> db -> query($sql);
		return $query;
	}
	
}
?>